﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Teigha.Core;
using Teigha.TD;

namespace YJKCopyModel.InteractiveOperation
{
	public class BlockInfo : BlockCadElement
	{
		public ImportInstance HostImportIns { get; set; }

        public string UseLayerName
        {
            get
            {
                return "";
            }
        }

		public Category RevitLayer { get; set; }

		public BlockInfo(OdDbBlockReference odDbBlockRef, ImportInstance ImportIns)
		{
			this.m_odDbBlockRef = odDbBlockRef;
			this.HostImportIns = ImportIns;
			this.UseLocationPoint = this.GetPos();
			this.UseScale = this.GetScale();
			this.Angle = this.GetRotation();
			this.BlockName = this.GetBlockName();
			this.GuidInCad = odDbBlockRef.handle().ascii();
            //this.UseLayerName = odDbBlockRef.layer();
			this.BoundingBoxCurve = this.GetBoungdingCurve();
		}

		protected XYZ GetPos()
		{
			OdGePoint3d cadPnt = this.m_odDbBlockRef.position();
			return GeometryCommon.ConvetPointFromCadToRevit(cadPnt, this.HostImportIns.GetTotalTransform());
		}

		protected XYZ GetScale()
		{
			OdGeScale3d odGeScale3d = this.m_odDbBlockRef.scaleFactors();
			return new XYZ(odGeScale3d.sx, odGeScale3d.sy, odGeScale3d.sz);
		}

		protected double GetRotation()
		{
			return this.m_odDbBlockRef.rotation();
		}

		protected string GetBlockName()
		{
			OdDbObject odDbObject = this.m_odDbBlockRef.blockTableRecord().safeOpenObject();
			bool flag = odDbObject == null;
			string result;
			if (flag)
			{
				result = string.Empty;
			}
			else
			{
				OdDbBlockTableRecord odDbBlockTableRecord = odDbObject as OdDbBlockTableRecord;
				result = odDbBlockTableRecord.getName();
			}
			return result;
		}

		protected List<Curve> GetBoungdingCurve()
		{
			List<Curve> list = new List<Curve>();
			try
			{
				OdGeExtents3d odGeExtents3d = new OdGeExtents3d();
				bool flag = this.m_odDbBlockRef.getGeomExtents(odGeExtents3d) == OdResult.eOk;
				if (flag)
				{
					OdGePoint3d odGePoint3d = odGeExtents3d.minPoint();
					odGePoint3d.z = 0.0;
					OdGePoint3d odGePoint3d2 = odGeExtents3d.maxPoint();
					odGePoint3d2.z = 0.0;
					OdGePoint3d cadPnt = new OdGePoint3d(odGePoint3d.x, odGePoint3d2.y, 0.0);
					OdGePoint3d cadPnt2 = new OdGePoint3d(odGePoint3d2.x, odGePoint3d.y, 0.0);
					List<XYZ> list2 = new List<XYZ>();
					Transform totalTransform = this.HostImportIns.GetTotalTransform();
					list2.Add(GeometryCommon.ConvetPointFromCadToRevit(odGePoint3d, totalTransform));
					list2.Add(GeometryCommon.ConvetPointFromCadToRevit(cadPnt, totalTransform));
					list2.Add(GeometryCommon.ConvetPointFromCadToRevit(odGePoint3d2, totalTransform));
					list2.Add(GeometryCommon.ConvetPointFromCadToRevit(cadPnt2, totalTransform));
					for (int i = 0; i < 4; i++)
					{
						XYZ xyz = list2[i];
						bool flag2 = i == 3;
						XYZ xyz2;
						if (flag2)
						{
							xyz2 = list2[0];
						}
						else
						{
							xyz2 = list2[i + 1];
						}
						Line item = Line.CreateBound(xyz, xyz2);
						list.Add(item);
					}
				}
			}
			catch (Exception)
			{
				return null;
			}
			return list;
		}

		protected OdDbBlockReference m_odDbBlockRef;
	}
}
